package com.example.wanbaqian.activity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.base.BaseListener;
import com.example.wanbaqian.utils.HttpUtils;
import com.joanzapata.pdfview.PDFView;

import java.io.File;

import butterknife.BindView;

/**
 * webview界面
 */
public class WebViewActivity extends BaseActivity{
    private static final String TAG = "WebViewActivity";
    @BindView(R.id.webView)
    WebView webView;

    private String title = "";
    private String webViewURL = "";

    @Override
    protected int getLayout() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        title = getIntent().getExtras().getString("title");
        webViewURL = getIntent().getExtras().getString("url");
        setStatusBarColor(R.color.main_color);
        setToolBar(title);



        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.getUrl().equals(webViewURL)) {
                    finish();
                }
                else if (title.equals("芝麻信用")) {
                    finish();
                }
                else {
                    webView.goBack();
                }
            }
        });

        Log.i(TAG, webViewURL);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);//关键点
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setDisplayZoomControls(true);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
        webSettings.setSupportZoom(true); // 支持缩放
        webSettings.setLoadWithOverviewMode(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //该方法在Build.VERSION_CODES.LOLLIPOP以前有效，
                //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
                //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，
                if (title.equals("芝麻信用")) {
                    if (url.toString().contains("apply/supplement.do")) {
                        finish();
                        return true;
                    }
                }
                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //该方法在Build.VERSION_CODES.LOLLIPOP以后有效
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    if (title.equals("芝麻信用")) {
                        if (request.getUrl().toString().contains("apply/supplement.do")) {
                            finish();
                            return true;
                        }
                    }
                }
                return false;
            }
        });
        Log.e(TAG, "webView加载网址: "+webViewURL);
        webView.loadUrl(webViewURL);
    }

    @Override
    public void onBackPressed() {
        if (webView.getUrl().equals(webViewURL)) {
            finish();
        } else if (title.equals("芝麻信用")) {
            finish();
        } else {
            webView.goBack();
        }
    }

    @Override
    protected void initDate() {

    }


}
